package com.brainshare.system.service.impl;

import java.util.Collections;
import java.util.List;

import ch.qos.logback.classic.net.SyslogAppender;
import cn.hutool.core.collection.CollectionUtil;
import com.brainshare.common.utils.DateUtils;
import com.brainshare.system.mapper.SysBannerItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.brainshare.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.brainshare.system.domain.SysBannerItem;
import com.brainshare.system.mapper.SysBannerMapper;
import com.brainshare.system.domain.SysBanner;
import com.brainshare.system.service.ISysBannerService;

/**
 * 活动内容Service业务层处理
 * 
 * @author wolong
 * @date 2023-03-13
 */
@Service
public class SysBannerServiceImpl implements ISysBannerService 
{
    @Autowired
    private SysBannerMapper sysBannerMapper;
    @Autowired
    private SysBannerItemMapper sysBannerItemMapper;

    /**
     * 查询活动内容
     * 
     * @param bannerId 活动内容主键
     * @return 活动内容
     */
    @Override
    public SysBanner selectSysBannerByBannerId(Long bannerId)
    {
        return sysBannerMapper.selectSysBannerByBannerId(bannerId);
    }

    /**
     * 查询活动内容列表
     * 
     * @param sysBanner 活动内容
     * @return 活动内容
     */
    @Override
    public List<SysBanner> selectSysBannerList(SysBanner sysBanner)
    {
        List<SysBanner> lstBanner = sysBannerMapper.selectSysBannerList(sysBanner);
        for (SysBanner banner : lstBanner)
        {
            SysBannerItem getBannerById = new SysBannerItem();
            getBannerById.setBannerId(banner.getBannerId());
            List<SysBannerItem> lstBannerItem = sysBannerItemMapper.selectSysBannerItemList(getBannerById);
            banner.getSysBannerItemList().addAll(lstBannerItem);
        }
        return lstBanner;
    }

    /**
     * 新增活动内容
     * 
     * @param sysBanner 活动内容
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysBanner(SysBanner sysBanner)
    {
        sysBanner.setCreateTime(DateUtils.getNowDate());
        int rows = sysBannerMapper.insertSysBanner(sysBanner);
        insertSysBannerItem(sysBanner);
        return rows;
    }

    /**
     * 修改活动内容
     * 
     * @param sysBanner 活动内容
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysBanner(SysBanner sysBanner)
    {
        sysBanner.setUpdateTime(DateUtils.getNowDate());
        sysBannerMapper.deleteSysBannerItemByBannerId(sysBanner.getBannerId());
        insertSysBannerItem(sysBanner);
        return sysBannerMapper.updateSysBanner(sysBanner);
    }

    /**
     * 批量删除活动内容
     * 
     * @param bannerIds 需要删除的活动内容主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysBannerByBannerIds(Long[] bannerIds)
    {
        sysBannerMapper.deleteSysBannerItemByBannerIds(bannerIds);
        return sysBannerMapper.deleteSysBannerByBannerIds(bannerIds);
    }

    /**
     * 删除活动内容信息
     * 
     * @param bannerId 活动内容主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysBannerByBannerId(Long bannerId)
    {
        sysBannerMapper.deleteSysBannerItemByBannerId(bannerId);
        return sysBannerMapper.deleteSysBannerByBannerId(bannerId);
    }

    /**
     * 新增活动内容明细信息
     * 
     * @param sysBanner 活动内容对象
     */
    public void insertSysBannerItem(SysBanner sysBanner)
    {
        List<SysBannerItem> sysBannerItemList = sysBanner.getSysBannerItemList();
        Long bannerId = sysBanner.getBannerId();
        if (StringUtils.isNotNull(sysBannerItemList))
        {
            List<SysBannerItem> list = new ArrayList<SysBannerItem>();
            for (SysBannerItem sysBannerItem : sysBannerItemList)
            {
                sysBannerItem.setCreateTime(DateUtils.getNowDate());
                sysBannerItem.setBannerId(bannerId);
                list.add(sysBannerItem);
            }
            if (list.size() > 0)
            {
                sysBannerMapper.batchSysBannerItem(list);
            }
        }
    }
}
